package io.adrainty.algorithm.leetcode.normal;

/**
 * @author adrainty
 * @version v1.0.0
 * @since 2024/12/23 上午10:49
 */
public class Solution7 {

    private Solution7() {}

    public static int reverse(int x) {
        // 2147483647
        // -2147483648
        if (x == 0) return 0;
        int res = 0;
        while (x != 0) {
            int digit = x % 10;
            if ((res > Integer.MAX_VALUE / 10) || (res == Integer.MAX_VALUE / 10 && digit > 7)) {
                return 0;
            }
            if ((res < Integer.MIN_VALUE / 10) || (res == Integer.MIN_VALUE / 10 && digit < -8)) {
                return 0;
            }
            res = res * 10 + digit;
            x = x / 10;
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(reverse(123));
        System.out.println(reverse(1534236469));
    }

}
